package cn.com.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.com.domain.Person;
import cn.com.service.PersonService;


@Controller
public class PersonController {
	@Resource
	PersonService ps;			//注入service层

	//列表
	@RequestMapping(value="/person/all")
	public String findAll(Map<String, Object> model){			//声明Model来传递数据
		List<Person> personList = ps.findAll();
		model.put("personList", personList);					//jsp页面中就可以访问personList
		
		return "/person/jPersonList";			//转向到list页面
	}
	
	//转向新增页面
		@RequestMapping("/person/tocreate")
		public String tocreate(){
			return "/person/jPersonCreate";
		}
	
		//转向修改页面
		@RequestMapping("/person/toupdate")
		public String toupdate(Integer id, Model model){
			//获得要修改的记录，重新设置页面提供的值
			Person p = ps.get(id);
			model.addAttribute("p", p);				//数据放到response
			
			return "/person/jPersonUpdate";
		}
		
		//新增和保存
		@RequestMapping("/person/saveorUpdate")
		public String saveorUpdate(Person p){
			if(p.getId()==null){
				ps.insert(p);
			}else{
				ps.update( p);
			}
			
			return "redirect:/person/all.action";
		}
		
		//删除
		@RequestMapping("/person/deleteById")
		public String deleteById(Integer id){
			ps.deleteById(id);						//删除方法
			
			return "redirect:/person/all.action";	//转向列表action	
		}
	
		//批量删除
		@RequestMapping("/person/deleteMuch")
		public String deleteMuch(String id){
			for(String delId : id.split(",")){
				ps.deleteById(Integer.parseInt(delId));
			}
			return "redirect:/person/all.action";	//转向列表action	
		}
			
}
